﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Tetris game by team Coconut</title>

    <script src="shims/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="shims/deepClone.js"></script>
    <script src="shims/ClassicalOop.js"></script>
    <script src="tetris.js"></script>
    <script src="Vector2D.js"></script>
    <script src="Block.js"></script>
    <script src="_FallingObject.js"></script>
    <script src="FallingL.js"></script>
    <script src="FallingLReversed.js"></script>
    <script src="FallingZ.js"></script>
    <script src="FallingZReversed.js"></script>
    <script src="FallingLine.js"></script>
    <script src="FallingSquare.js"></script>
    <script src="FallingT.js"></script>
    <script src="Earth.js"></script>
    <script src="CollisionType.js"></script>
    <script src="CollisionDetector.js"></script>
    <script src="Renderer.js" type="text/javascript"></script>
    <script src="Factory.js" type="text/javascript"></script>

</head>
<body>
    <div id="field" style="width:300px;height:500px;border:2px solid orange;"></div>
    <div id="figure" style="width:150px;height:150px;border:2px solid orange;"></div>
    <script>
    (function () {
        var Engine = function (fieldId, nextFallingId, scoreId, levelId) {
            this.isPlaying = false;
            this.isAnimating = false;
            this.pressedKey = "";
            this.renderer = new tetris.Renderer(fieldId, nextFallingId, scoreId, levelId);
            this.colider = new tetris.CollisionDetector();
            this.factory = new tetris.Factory();

            this.rowsToDelete = [];
            var self = this;
            this.earth = new tetris.Earth(6, 9, function (index) {
                self.rowsToDelete.push(index);
            });
            this.falling = this.factory.createFallingObject
                (this.renderer._blockFallingCenterX, this.renderer._blockFallingCenterY);
            this.nextFalling = this.factory.createFallingObject
                (this.renderer._nextFallingCenter, this.renderer._nextFallingCenter);
            this.score = 0;
            this.level = 1;
            this.speed = 500;
        }
        Engine.prototype.pausePlay = function () {
            this.isPlaying = !this.isPlaying;
        };
        Engine.prototype._checkCollisionsAndReverse = function (collisions) {
            var reverseBack = false;

            if (collisions[tetris.CollisionType.Earth]) {
                reverseBack = true;
            }

            if (collisions[tetris.CollisionType.TopEdge]) {
                reverseBack = true;
                this.isPlaying = false;
            }

            if (collisions[tetris.CollisionType.LeftEdge]) {
                reverseBack = true;
            }

            if (collisions[tetris.CollisionType.RightEdge]) {
                reverseBack = true;
            }

            return reverseBack;
        };
        Engine.prototype.readUserKeyAndGetUpdateInfo = function () {
            var collisions = {};
            var reverse = false;
            var updateEarth = false;

            switch (this.pressedKey) {
                case "38": //Up
                    this.falling.rotate();
                    collisions = this.colider.getCollisions(this.falling, this.earth);
                    reverse = this._checkCollisionsAndReverse(collisions);
                    if (reverse) {
                        this.falling.rotateBack();
                    }
                    break;
                case "37": //Left
                    this.falling.moveLeft();
                    collisions = this.colider.getCollisions(this.falling, this.earth);
                    reverse = this._checkCollisionsAndReverse(collisions);
                    if (reverse) {
                        this.falling.moveRight();
                    }
                    break;
                case "39": //Right
                    this.falling.moveRight();
                    collisions = this.colider.getCollisions(this.falling, this.earth);
                    reverse = this._checkCollisionsAndReverse(collisions);
                    if (reverse) {
                        this.falling.moveLeft();
                    }
                    break;
                case "40": //Down
                    this.falling.moveDown();
                    collisions = this.colider.getCollisions(this.falling, this.earth);
                    reverse = this._checkCollisionsAndReverse(collisions);
                    if (reverse) {
                        this.falling.moveUp();
                    }
                    break;
            }

            if (collisions[tetris.CollisionType.Earth]) {
                updateEarth = true;
            }

            return updateEarth;
        };
        Engine.prototype.playCycleTurn = function () {
            if (this.isPlaying) {
                this.pressedKey = "40";
                var updateEarth = this.readUserKeyAndGetUpdateInfo();
                if (updateEarth) {
                    this.earth.add(this.falling);
                    this.earth.updateBodyAndReturnCollapsedRowsCount();

                    this.nextFalling.moveTo
                        (this.renderer._blockFallingCenterX, this.renderer._blockFallingCenterY);
                    this.falling = this.nextFalling;

                    this.nextFalling = this.factory.createFallingObject
                        (this.renderer._nextFallingCenter, this.renderer._nextFallingCenter);

                    while (this.rowsToDelete.length > 0) {
                        var row = this.rowsToDelete.pop();
                        this.renderer.eraseRowAnimation(row, 200, this);
                        this.isAnimating = true;
                        while (this.isAnimating) {
                            console.log("animating...");
                        }
                    }

                    this.renderer.render(this.falling, this.nextFalling, this.earth);
                }
                else {
                    this.renderer.renderFalling(this.falling);
                }

                this.pressedKey = "";
                var self = this;
                setTimeout(function () { self.playCycleTurn(); }, this.speed);
            }
        };
        Engine.prototype.start = function () {
            var self = this;
            this.isPlaying = true;

            window.onkeydown = function (ev) {
                self.pressedKey = ev.keyCode.toString();
            };

            this.renderer.render(this.falling, this.nextFalling, this.earth);
            this.playCycleTurn();
        };

        tetris.Engine = Engine;
    })();
    </script>

    <script>
        $("#field").css("position", "relative");
        $("#figure").css("position", "relative");

        var engine = new tetris.Engine("field", "figure", "score", "level");
        engine.start();

//        var factory = engine.factory;

//        var renderer = engine.renderer;

//        var fL = factory.createFallingObject();
//        fL.moveTo(renderer._nextFallingCenter, renderer._nextFallingCenter);
//        renderer.renderNextFalling(fL);
//        renderer.renderFalling(fL);

//        fL.rotate();
//        renderer.renderNextFalling(fL);

    </script>
</body>
</html>
